package cst.neu.yuejian_31.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.activity.VideoActivity;
import cst.neu.yuejian_31.bean.VideoBean;
import cst.neu.yuejian_31.Common.Constant;

/**
 * Created by xxe on 2017/11/1.
 */

public class VideoAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;
    private List<VideoBean> list;

    public VideoAdapter(Context context){
        this.context=context;
        layoutInflater=LayoutInflater.from(context);

    }
    public void setList(List<VideoBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView videoName,videoIntro,videoTime;
        private ImageView videoPic;
        public ViewHolder(View itemView) {
            super(itemView);
            videoName= (TextView) itemView.findViewById(R.id.textView_vidername);
            videoIntro= (TextView) itemView.findViewById(R.id.textView_videointro);
            videoTime= (TextView) itemView.findViewById(R.id.textView_vidertime);
            videoPic= (ImageView) itemView.findViewById(R.id.imageView_videopic);

        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=layoutInflater.inflate(R.layout.item_video,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final VideoBean videoBean=list.get(position);
        if(videoBean==null)
            return;
        ViewHolder viewHolder= (ViewHolder) holder;
        viewHolder.videoName.setText(videoBean.getClubmovie_name());
        viewHolder.videoIntro.setText(videoBean.getClubmovie_introduce());
        viewHolder.videoTime.setText(videoBean.getClubmovie_uptime());
        Picasso.with(context).load(Constant.VIDELPIC_URL+videoBean.getClubmovie_pic().toString()).into(viewHolder.videoPic);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取当前条目信息，如id
                String id=videoBean.getClubmovie_id();
                Toast.makeText(context,""+id, Toast.LENGTH_SHORT).show();
                //实际中，点击后打开新窗口做其他操作
                Intent intent=new Intent(context,VideoActivity.class);
                intent.putExtra("videopath",videoBean.getClubmovie_movie());
                intent.putExtra("resid",videoBean.getClubmovie_id());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (list==null)
            return 0;
        else {
            return list.size();
        }
    }
}
